﻿using AutoMapper;
using IMES.Common.Domain.KeepModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace Keep.API.Appliction.Command.WarehouseFile
{
    public class WarehouseCreateHandler:IRequestHandler<WarehouseCreateCommon,Result>
    {
        private readonly IBaseRepositorys<WarehouseManagement> _WarehouseManagementRep;
        private readonly IMapper _mapper;
        public WarehouseCreateHandler(IBaseRepositorys<WarehouseManagement> warehouseManagementRep, IMapper mapper)
        {
            _WarehouseManagementRep = warehouseManagementRep;
            _mapper = mapper;
        }
        /// <summary>
        /// 创建仓库
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<Result> Handle(WarehouseCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.success,Messege = "创建仓库成功" };
            try
            {
                var warehouse = _mapper.Map<WarehouseManagement>(request);
                var n =  _WarehouseManagementRep.Add(warehouse);
                if (n == 0)
                {
                    result.Code = (int)CodeEnum.add;
                    result.Messege = "创建仓库失败";
                }
                return Task.FromResult(result);

                
            }
            catch (Exception ex)
            {
                result.Code = (int)CodeEnum.error;
                result.Messege = ex.Message;
            }
            return Task.FromResult(result);
        }
    }
}
